package com.situ.day20.db;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class DBFactory {

    public static IDB getDB() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1.通过当前的类获取类加载器
        ClassLoader classLoader = DBDemo.class.getClassLoader();
        //2.通过类加载器的方法获取输入流
        InputStream inputStream = classLoader.getResourceAsStream("db1.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String className = properties.getProperty("className");
        //com.situ.day20.db.MySql
        Class clazz = Class.forName(className);
        Constructor constructor = clazz.getConstructor();
        IDB db = (IDB) constructor.newInstance();
        db.getConnection();
        return db;
    }

    public static IDB getDB(int type) {
        IDB db = null;
        switch (type) {
            case 1:
                db = new MySql();
                break;
            case 2:
                db = new SqlServer();
                break;
            case 3:
                db = new Oracle();
                break;
        }
        return db;
    }
}
